﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_QToolEdit : DevExpress.XtraEditors.XtraForm
	{
		public string QToolId = "";
		public string MakerId = "";
		public string RRIToolName = "";
		public string MakerToolName = "";
		public int Price = 0;
		public int SafeQty = 0;
		public string FrequencyId = "";
		public string ToolGroupId = "";
		private ucQTool parent;

		public frm_QToolEdit(ucQTool fTool)
		{
			InitializeComponent();
			parent = fTool;
			this.CancelButton = this.btCancel;
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			QToolHelper helper = new QToolHelper(new QTool());
			if(GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_ToolEdit_Load(object sender, EventArgs e)
		{
			BindData();
			BindDataToFrequency();
			BindDataToToolGroup();
			BindDataToMakers();
		}

		#region Private methods

		private bool CheckDuplicate(string id)
		{
			QToolHelper helper = new QToolHelper(new QTool());
			helper.ValueObject.QToolID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}

		private bool GetData(QTool o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtToolID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.QTool.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtRRIName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.QTool.ToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtMakerToolname.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.QTool.MakerToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.QTool.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtSafeQty.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.QTool.SafeQtyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(string.Compare(txtToolID.Text.Trim(), QToolId) != 0)
			{
				if(CheckDuplicate(txtToolID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.QTool.ToolHasExisted, ToolConstants.Form.MessageBox.Error,
									MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.QToolID = txtToolID.Text;
			o.MakerName = txtMakerToolname.Text;
			o.RRIName = txtRRIName.Text;
			o.MakerID = cmbMakerID.SelectedValue.ToString();
			o.Price = int.Parse(txtPrice.Text);
			o.SafeQty = int.Parse(txtSafeQty.Text);
			o.FrequencyID = cmbFrequency.SelectedValue.ToString();
			o.ToolGroupID = cmbToolGroup.SelectedValue.ToString();

			return true;
		}


		private void BindData()
		{
			txtToolID.Text = QToolId;
			txtMakerToolname.Text = MakerToolName;
			txtRRIName.Text = RRIToolName;
			txtPrice.Text = Price.ToString();
			txtSafeQty.Text = SafeQty.ToString();
		}

		private void BindDataToFrequency()
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			cmbFrequency.DataSource = helper.GetAll();
			cmbFrequency.SelectedValue = FrequencyId;
		}

		private void BindDataToToolGroup()
		{
			ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
			cmbToolGroup.DataSource = helper.GetAll();
			cmbToolGroup.SelectedValue = ToolGroupId;

		}

		private void BindDataToMakers()
		{
			MakerHelper helper = new MakerHelper(new Maker());
			cmbMakerID.DataSource = helper.GetAll();
			cmbMakerID.SelectedValue = MakerId;

		}

		#endregion

	}
}